package com.apps.ftapp.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.apps.ftapp.R;
import com.apps.ftapp.bean.ZigezhengBean;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.List;

public class NewZpAdapter extends ArrayAdapter<ZigezhengBean> {


    private int resource;
    private Context context;
    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions options;


    public NewZpAdapter(Context context, int resource, List<ZigezhengBean> objects) {

        super(context, resource, objects);
        this.resource = resource;
        this.context = context;
        //显示图片的配置  
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher).cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();
        imageLoader.init(ImageLoaderConfiguration.createDefault(getContext()));
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {


        ZigezhengBean bean = getItem(position);
        ViewHolder holder = null;
        if (view == null) {
            view = LayoutInflater.from(getContext()).inflate(resource, null, false);
            holder = new ViewHolder();
            holder.bookImg = view.findViewById(R.id.book_img);
            holder.price = view.findViewById(R.id.price);
            holder.saleNum = view.findViewById(R.id.sale_num);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.price.setText("¥" + bean.getPrice());
        holder.saleNum.setText("已购：" + bean.getStudentNum());
        imageLoader.displayImage(bean.getLargePicture(), holder.bookImg, options);

        return view;
    }


    private class ViewHolder {
        ImageView bookImg;
        TextView price;
        TextView saleNum;
    }


}
